Imports System.Windows.Forms
Imports System.IO
Public Class dlgEmployee

    Public EmployeeID As String
    Public _getlastcard As String = getLastCardSQL()
    Public Sub setActionName(ByVal caption As String)
        btnNew.Text = caption
    End Sub



    Private Sub TimerE_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TimerE.Tick
        Dim CardNumber As String = ""
        'Neu quet the, tim so card number
        TimerE.Enabled = False 'Ngung doc the
        CardNumber = getLastCard(_getlastcard)
        If CardNumber.Length > 0 Then
            txtCardNumber.Text = CardNumber
        End If
        TimerE.Enabled = True 'Ngung doc the
    End Sub

    Private Sub btnNew_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNew.Click
        Dim sql As String = ""
        Try
            cbDepartment.SelectedItem.item(0)
        Catch ex As Exception
            MsgBox("Pls set Department")
            Exit Sub
        End Try
        Try
            cbLocation.SelectedItem.item(0)
        Catch ex As Exception
            MsgBox("Pls set Location")
            Exit Sub
        End Try
        Try
            cbType.SelectedItem.item(0)
        Catch ex As Exception
            MsgBox("Pls set Type")
            Exit Sub
        End Try
        If txtCardNumber.Text.Length > 0 Then
            Dim rs As DataSet
            rs = db.getvalue("select * from employee where ecardnumber  = '" & txtCardNumber.Text & "' and employeeid<>'" & txtEmployeeID.Text & "'")
            Dim id As String = ""
            If rs.Tables(0).Rows.Count > 0 Then

                Dim i As Integer
                For i = 0 To rs.Tables(0).Rows.Count - 1
                    id = id & Chr(13) & rs.Tables(0).Rows(i).Item("Employeeid") & "-" & rs.Tables(0).Rows(i).Item("eFullname")
                Next
                If id <> "" Then
                    If MsgBox("The following people will be reset their Card Number to empty: " & id, MsgBoxStyle.YesNo) = MsgBoxResult.Yes Then
                        db.update("update employee set eCardNumber = '' where ecardnumber = '" & txtCardNumber.Text.Replace("'", "''") & "' and employeeID <> '" & txtEmployeeID.Text.Replace("'", "''") & "'")
                    Else
                        MsgBox("Cannot save!!!")
                        Exit Sub
                    End If
                End If
            End If
        End If
        If btnNew.Text.ToLower = "new" Then

            If txtEmployeeID.Text.Length <= 0 Then
                MsgBox("EmployeeID is blank!!!")
                Exit Sub
            End If
            If db.getvalue("select * from employee where Employeeid = '" & txtEmployeeID.Text.Replace("'", "") & "'").Tables(0).Rows.Count > 0 Then
                MsgBox("Duplicate!!!")
                Exit Sub
            End If

            sql = "insert into employee(EmployeeID,eFullname,eDepartment,eLocation,etype, " _
                & "eCardNumber,eLimit,eActive,eCreatedby) " _
                & " values('" & txtEmployeeID.Text.Replace("'", "''") _
                & "','" & txtFullName.Text.Replace("'", "''") _
                & "','" & cbDepartment.SelectedItem.item(0) _
                & "','" & cbLocation.SelectedItem.item(0) _
                & "','" & cbType.SelectedItem.item(0) _
                & "','" & txtCardNumber.Text.Replace("'", "''") _
                & "'," & chkLimit.Checked * -1 _
                & "," & chkActive.Checked * -1 _
                & ",'" & LoginForm.userID _
                & "')"
        End If
        If btnNew.Text.ToLower = "update" Then
            'check exist
            If db.getvalue("select * from employee where Employeeid = '" & txtEmployeeID.Text.Replace("'", "") & "'").Tables(0).Rows.Count = 0 Then
                MsgBox("EmployeeID is not exist!!!")
                Exit Sub
            End If

            sql = "update employee set eFullname = '" & txtFullName.Text.Replace("'", "''") _
                & "',eDepartment='" & cbDepartment.SelectedItem.item(0) _
                & "',eLocation='" & cbLocation.SelectedItem.item(0) _
                & "',etype='" & cbType.SelectedItem.item(0) _
                & "',eCardNumber='" & txtCardNumber.Text.Replace("'", "''") _
                & "',eLimit=" & chkLimit.Checked * -1 _
                & ",eActive=" & chkActive.Checked * -1 _
                & ",eCreatedby='" & LoginForm.userID _
                & "' where EmployeeID = '" & txtEmployeeID.Text.Replace("'", "''") & "'"
        End If
        Try

            db.update(sql)
            If db.getvalue("select * from cardnumber where cardnumberid = '" & txtCardNumber.Text & "'").Tables(0).Rows.Count = 0 Then
                db.update("insert into CardNumber(CardNumberID,cActive,cCreatedby) " _
                & " values('" & txtCardNumber.Text & "',1,'" & LoginForm.userID & "')")
            End If
            If Not pbEmployee.Image Is Nothing Then
                Dim mstr As New MemoryStream()
                pbEmployee.Image.Save(mstr, pbEmployee.Image.RawFormat)
                Dim arrImage As Byte() = mstr.GetBuffer()
                db.InsertImageToEmployee(mstr.GetBuffer(), txtEmployeeID.Text.Replace("'", "''"))

            End If

            MsgBox("The EmployeeID " & txtEmployeeID.Text & " has been saved.")

        Catch ex As Exception
            MsgBox(ex.Message)
        End Try

    End Sub
    Private Sub initValue(ByVal employeeID As String)
        Dim ds As DataSet
        ds = db.getvalue("select * from Employee where EmployeeID = '" & employeeID & "'")
        On Error Resume Next
        txtFullName.Text = ""
        txtCardNumber.Text = ""
        chkLimit.Checked = True
        chkActive.Checked = True
        pbEmployee.Image = Nothing
        cbDepartment.DataSource = db.getvalue("select * from Department").Tables(0)
        cbDepartment.DisplayMember = "dep_Name"
        cbDepartment.ValueMember = "DepartmentID"

        cbLocation.DataSource = db.getvalue("select * from Location").Tables(0)
        cbLocation.DisplayMember = "Loc_name"
        cbLocation.ValueMember = "LocationID"

        cbType.DataSource = db.getvalue("select * from ETYPE").Tables(0)
        cbType.DisplayMember = "TypeName"
        cbType.ValueMember = "TypeID"

        

        If ds.Tables(0).Rows.Count > 0 Then

            txtEmployeeID.Text = ds.Tables(0).Rows(0).Item("EmployeeID").ToString
            txtFullName.Text = ds.Tables(0).Rows(0).Item("eFullName").ToString

            cbDepartment.SelectedIndex = FindIndex(cbDepartment, ds.Tables(0).Rows(0).Item("eDepartment").ToString, False)
            cbLocation.SelectedIndex = FindIndex(cbLocation, ds.Tables(0).Rows(0).Item("eLocation").ToString, False)
            cbType.SelectedIndex = FindIndex(cbType, ds.Tables(0).Rows(0).Item("etype").ToString, False)
            txtCardNumber.Text = ds.Tables(0).Rows(0).Item("eCardNumber").ToString
            chkLimit.Checked = ds.Tables(0).Rows(0).Item("eLimit").ToString
            chkActive.Checked = ds.Tables(0).Rows(0).Item("eActive").ToString

            Dim b As Byte()
            b = CType(ds.Tables(0).Rows(0).Item("epicture"), Byte())
            Dim mem As New MemoryStream(b)
            pbEmployee.Image = Image.FromStream(mem)

        End If

    End Sub
    Private Sub dlgEmployee_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        txtEmployeeID.Text = ""
        CheckBox1.Checked = False
        initValue(EmployeeID)
        EmployeeID = ""

        'Dim tooltip As New ToolTip
        'tooltip.SetToolTip(pbEmployee, "hello")
        ToolTip1.SetToolTip(pbEmployee, "LeftClick to select new picture" & Chr(13) & "RightClick to enlarge")
        'ToolTip1.SetToolTip(nupQuantity, "For new comer, set actual coupons for current quarter")

    End Sub
    

    Private Sub pbEmployee_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles pbEmployee.MouseClick
        If e.Button = Windows.Forms.MouseButtons.Left Then
            Dim OpenFileDialog1 As New OpenFileDialog
            With OpenFileDialog1
                '.InitialDirectory = "C:\"
                .Filter = "All Files|*.*|Bitmaps|*.bmp|GIFs|*.gif|JPEGs|*.jpg"
                .FilterIndex = 4
            End With

            If OpenFileDialog1.ShowDialog() = DialogResult.OK Then
                pbEmployee.ImageLocation = OpenFileDialog1.FileName
            End If
        End If
        If e.Button = Windows.Forms.MouseButtons.Right Then
            If Not pbEmployee.Image Is Nothing Then
                dlgImage.BackgroundImage = pbEmployee.Image
                dlgImage.ShowDialog()
            End If
        End If
    End Sub

    'Private Sub chkLimit_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkLimit.CheckedChanged

    '    nupQuantity.Enabled = chkLimit.Checked
    'End Sub


    Private Sub CheckBox1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox1.CheckedChanged
        CheckBox1.Text = IIf(CheckBox1.Checked, "Stop", "Find Card")
        TimerE.Enabled = CheckBox1.Checked
    End Sub

    Private Sub txtEmployeeID_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtEmployeeID.Leave

        If db.getvalue("select * from employee where Employeeid = '" & txtEmployeeID.Text.Replace("'", "") & "'").Tables(0).Rows.Count > 0 Then
            btnNew.Text = "Update"
            initValue(txtEmployeeID.Text.Replace("'", ""))
        Else
            btnNew.Text = "New"
            initValue(txtEmployeeID.Text.Replace("'", ""))
        End If
    End Sub

    Private Sub txtCardNumber_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtCardNumber.Leave

    End Sub

    'Private Sub txtEmployeeID_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtEmployeeID.TextChanged
    '    If btnNew.Text.ToLower = "new" Then
    '        If db.getvalue("select * from employee where Employeeid = '" & txtEmployeeID.Text.Replace("'", "") & "'").Tables(0).Rows.Count > 0 Then
    '            'lblcheckID.Text = "Duplicate!!!"
    '            initValue(txtEmployeeID.Text.Replace("'", ""))
    '            btnNew.Text = "Update"
    '        Else
    '            lblcheckID.Text = ""
    '            'Exit Sub
    '        End If
    '    End If


    'End Sub



End Class
